/**
 * Copyright &copy; 2016-2017 <a href="https://www.lczyfz.com">联创智云</a> All rights reserved.
 */
package com.lczyfz.campusinfo.modules.campusinfomodules.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lczyfz.campusinfo.common.utils.Const;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lczyfz.campusinfo.common.config.Global;
import com.lczyfz.campusinfo.common.persistence.Page;
import com.lczyfz.campusinfo.common.web.BaseController;
import com.lczyfz.campusinfo.common.utils.StringUtils;
import com.lczyfz.campusinfo.modules.campusinfomodules.entity.ModuleDetail;
import com.lczyfz.campusinfo.modules.campusinfomodules.service.ModuleDetailService;

/**
 * 模块Controller
 * @author zms
 * @version 2018-04-10
 */
@Controller
@RequestMapping(value = "${adminPath}/campusinfomodules/moduleDetail")
public class ModuleDetailController extends BaseController {

	@Autowired
	private ModuleDetailService moduleDetailService;
	
	@ModelAttribute
	public ModuleDetail get(@RequestParam(required=false) String id) {
		ModuleDetail entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = moduleDetailService.get(id);
		}
		if (entity == null){
			entity = new ModuleDetail();
		}
		return entity;
	}
	
	@RequiresPermissions("campusinfomodules:moduleDetail:view")
	@RequestMapping(value = {"list", ""})
	public String list(ModuleDetail moduleDetail, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ModuleDetail> page = moduleDetailService.findPage(new Page<ModuleDetail>(request, response), moduleDetail); 
		model.addAttribute("page", page);
		return "modules/campusinfomodules/moduleDetailList";
	}

	@RequiresPermissions("campusinfomodules:moduleDetail:view")
	@RequestMapping(value = "form")
	public String form(ModuleDetail moduleDetail, Model model) {
		model.addAttribute("moduleDetail", moduleDetail);
		return "modules/campusinfomodules/moduleDetailForm";
	}

	@RequiresPermissions("campusinfomodules:moduleDetail:edit")
	@RequestMapping(value = "save")
	public String save(ModuleDetail moduleDetail, Model model,  HttpServletRequest request,RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, moduleDetail)){
			return form(moduleDetail, model);
		}
		if(StringUtils.isNotBlank(moduleDetail.getModuleImg())){
			moduleDetail.setModuleImg(Const.getinetAddressService(moduleDetail.getModuleImg(),request));
		}
		moduleDetailService.save(moduleDetail);
		addMessage(redirectAttributes, "保存模块成功");
		return "redirect:"+Global.getAdminPath()+"/campusinfomodules/moduleDetail/?repage";
	}
	
	@RequiresPermissions("campusinfomodules:moduleDetail:edit")
	@RequestMapping(value = "delete")
	public String delete(ModuleDetail moduleDetail, RedirectAttributes redirectAttributes) {
		moduleDetailService.delete(moduleDetail);
		addMessage(redirectAttributes, "删除模块成功");
		return "redirect:"+Global.getAdminPath()+"/campusinfomodules/moduleDetail/?repage";
	}

}